# -*- coding: utf-8 -*-
import scrapy
import json
from scrapy import Spider,Request
from zhuhuuser.items import UserItem
class ZhihuSpider(scrapy.Spider):
    name = 'zhihu'
    allowed_domains = ['www.zhihu.com']
    start_urls = ['http://www.zhihu.com/']

    starUser='excited-vczh'

    userUrl='https://www.zhihu.com/api/v4/members/{user}?include={include}'
    userQuery='allow_message,is_followed,is_following,is_org,is_blocking,employments,answer_count,follower_count,articles_count,gender,badge[?(type=best_answerer)].topics'
    
    followUrl='https://www.zhihu.com/api/v4/members/{user}/followees?include={include}&offset={offset}&limit={limit}'
    followQuery='data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'
    
    followerUrl='https://www.zhihu.com/api/v4/members/{user}/followers?include={include}&offset={offset}&limit={limit}'
    followerQuery='data[*].answer_count,articles_count,gender,follower_count,is_followed,is_following,badge[?(type=best_answerer)].topics'
    
    def start_requests(self):
        yield Request(self.userUrl.format(user=self.starUser,include=self.userQuery),self.parseUser)
        yield Request(self.followUrl.format(user=self.starUser,include=self.followQuery,offset=0,limit=20),callback=self.parseFollow)
        yield Request(self.followerUrl.format(user=self.starUser,include=self.followerQuery,offset=0,limit=20),callback=self.parseFollower)


    def parseUser(self, response):
        result=json.loads(response.text)
        item=UserItem()
        for field in item.fields:
            if field in result.keys():
                item[field]=result.get(field)
        yield item

        yield Request(self.followUrl.format(user=result.get('url_token'),include=self.followQuery,offset=0,limit=20),self.parseFollow)
        yield Request(self.followerUrl.format(user=result.get('url_token'),include=self.followQuery,offset=0,limit=20),self.parseFollower)
    
    def parseFollow(self, response):
        results=json.loads(response.text)
        if 'data' in results.keys():
            for result in results.get('data'):
                yield Request(self.userUrl.format(user=result.get('url_token'),include=self.userQuery),self.parseUser)

        
        if 'paging' in results.keys() and results.get('paging').get('is_end')==False:
            
            next_page=results.get('paging').get('next')
            yield Request(next_page,self.parseFollow)

    def parseFollower(self,response):
        results=json.loads(response.text)
        if 'data' in results.keys():
            for result in results.get('data'):
                yield Request(self.userUrl.format(user=result.get('url_token'),include=self.userQuery),self.parseUser)

        
        if 'paging' in results.keys() and results.get('paging').get('is_end')==False:
            
            next_page=results.get('paging').get('next')
            yield Request(next_page,self.parseFollower)